#include "mainscene.h"
#include "ui_mainscene.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <QMediaPlayer>
#include <QAudioOutput>

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);

    // 配置主场景

    // 设置固定大小
    setFixedSize(320, 588);

    // 设置图标
    setWindowIcon(QIcon(":/res/Coin0001.png"));

    // 设置标题
    setWindowTitle("翻金币主场景");

    connect(ui->actionQuit, &QAction::triggered, this, [=](){
        close();
    });

    // 开始按钮
    MyPushButton * startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(width() / 2 - startBtn->width() / 2, height() - startBtn->height());

    chooseScene = new ChooseLevelScene;
    // 监听选择关卡的返回按钮的信号
    connect(chooseScene, &ChooseLevelScene::chooseSceneBack, this, [=](){
        setGeometry(chooseScene->geometry());
        chooseScene->hide();
        show();
    });

    // 准备开始音乐音效
    QMediaPlayer* player = new QMediaPlayer(this);
    QAudioOutput *audioOutput = new QAudioOutput(this);
    player->setAudioOutput(audioOutput);
    audioOutput->setVolume(0.5);  //调节音频音量
    player->setSource(QUrl("qrc:/res/TapButtonSound.wav"));

    connect(startBtn, &MyPushButton::clicked, this, [=](){
        qDebug() << "点击开始了";
        //startBtn->zoom1();
        startBtn->zoom2();

        // 延时进入到选择关卡场景中
        QTimer::singleShot(500, this, [=](){
            // 设置初始位置
            chooseScene->setGeometry(geometry());
            // 自身隐藏
            hide();
            // 显示选择关卡场景
            chooseScene->show();
            player->play();
        });
    });
}

void MainScene::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    //painter.drawPixmap(0, 0, pix);
    painter.drawPixmap(0, 0, width(), height(), pix);

    // 画背景上图标
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width() * 0.5, pix.height() * 0.5);
    painter.drawPixmap(0, 0 , pix);
}

MainScene::~MainScene()
{
    delete ui;
}
